package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.dto.UserRoleDto;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import com.itheima.ydd.service.UserService;
import com.itheima.ydd.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("sys_user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;


    @GetMapping("/list")
    public ResultData list(String login_name,Integer status,String phone,int page,int limit){

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(StringUtils.isNotEmpty(login_name),User::getLogin_name,login_name);
        userLambdaQueryWrapper.eq(status != null,User::getStatus,status);
        userLambdaQueryWrapper.eq(StringUtils.isNotEmpty(phone),User::getPhone,phone);

        Page<User> userPage = new Page<>(page, limit);
        userService.page(userPage,userLambdaQueryWrapper);


        List<User> userList = userPage.getRecords();
        List<UserRoleDto> userRoleDtoList = userList.stream().map(s -> {
            UserRoleDto userRoleDto = new UserRoleDto();
            BeanUtils.copyProperties(s,userRoleDto);

            String role_id = s.getRole_id();
            role_id=role_id.substring(0,role_id.length()-2).substring(2);

            Role role = roleService.getById(role_id);

            String name = "[\""+role.getName()+"\"]";  // ["aaaaa"]
            userRoleDto.setRolename(name);

            return userRoleDto;

        }).collect(Collectors.toList());


        Page<UserRoleDto> userRoleDtoPage = new Page<>();
        BeanUtils.copyProperties(userPage,userRoleDtoPage);

        userRoleDtoPage.setRecords(userRoleDtoList);

        PageDto pageDto = PageUtils.getPageDto(userRoleDtoPage);

        return ResultData.ok("",pageDto);















        /*List<User> userList = userPage.getRecords();

        List<UserRoleDto> collect = userList.stream().map(s -> {

            String role_id = s.getRole_id();
            Role roleId = roleService.getById(role_id);
            UserRoleDto userRoleDto = new UserRoleDto();
            BeanUtils.copyProperties(s, userRoleDto);

            LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleMenuLambdaQueryWrapper.eq(RoleMenu::getRole_id, roleId);
            List<RoleMenu> roleMenuList = roleMenuService.list(roleMenuLambdaQueryWrapper);

            List<String> collect1 = roleMenuList.stream().map(s1 -> {
                String menu_id = s1.getMenu_id();
                return menu_id;

            }).collect(Collectors.toList());

            userRoleDto.setRole_menus(collect1);

            return userRoleDto;

        }).collect(Collectors.toList());*/






    }

}
